<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>登录</title>
    <script type="text/javascript" th:src="@{/jquery-3.6.0.js}"></script>
</head>
<body>
<div id="login">
    <h1>登录</h1>
    <form id="from">
        <input type="text" id="username" placeholder="用户名" name="username"></input>
        <input type="password" id="password" placeholder="密码" name="password"></input>
        <button id="btn_login" type="button">登录</button>
    </form>
</div>
<div>
    <a href="https://gitee.com/oauth/authorize?client_id=94ee878759c9550f79bf7e9c15293bacc3252bee711efef35e9dddd456f25cab&redirect_uri=http://localhost:57031/oauth.html&response_type=code">
        <img th:src="@{/gitee.png}">
    </a>
</div>
</body>
</html>
<script type="text/javascript">
    $(function () {
        $("#btn_login").click(function () {
            var username = $("#username").val();
            var password = $("#password").val();
            console.log(username, password);

            var login_data = {"username": username, "password": password}

            $.ajax({
                type: 'POST',
                url: 'http://localhost:57025/login',
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(login_data),
                dataType: 'json',
                success: function (result) {
                    console.log('请求成功！');
                    //console.log(result);

                    // 清除本地存储
                    localStorage.removeItem("shop.userId")
                    localStorage.removeItem("shop.nickName")
                    localStorage.removeItem("shop.token")
                    // 保存用户名到本地存储
                    localStorage.setItem("shop.userId", result.data.user.id);
                    localStorage.setItem("shop.nickName", result.data.user.nickName);
                    // 保存token到本地存储
                    localStorage.setItem("shop.token", result.data.token);
                },
                error: function (errordata) {
                    console.log('请求出错！');
                    console.log(errordata);
                }
            })
        });
    });
</script>